package chapter14;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author : DuJiabao
 * @Date : 2021/2/25 15:54
 * @Project : thinking_in_java
 * @File : null.java
 * @Desc :
 */
class MethodSelector implements InvocationHandler {
  private Object proxied;

  public MethodSelector(Object proxied) {
    this.proxied = proxied;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (method.getName().equals("interesting"))
      System.out.println("Proxy detected the interesting method");
    return method.invoke(proxied, args);
  }
}

interface SomeMethods {
  void boring1();

  void boring2();

  void boring3();

  void interesting(String arg);
}

class Implementation implements SomeMethods {
  public void boring1() {
    System.out.println("boring1");
  }

  public void boring2() {
    System.out.println("boring2");
  }

  public void boring3() {
    System.out.println("boring3");
  }

  public void interesting(String arg) {
    System.out.println("interesting " + arg);
  }
}

public class SelectingMethods {
  public static void main(String[] args) {
    SomeMethods proxy = (SomeMethods) Proxy.newProxyInstance(
            SomeMethods.class.getClassLoader(),
            new Class[]{SomeMethods.class},
            new MethodSelector(new Implementation()));
    proxy.boring1();
    proxy.boring2();
    proxy.boring3();
    proxy.interesting("bonobo");
  }
}
